package com.crrc.yzt.demo.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.crrc.yzt.demo.adapter.ValueAdapter3
import com.crrc.yzt.demo.base.BaseActivity
import com.crrc.yzt.demo.blue.databinding.ActivityValue3Binding
import com.crrc.yzt.demo.vm.CarValueVM
import kotlinx.coroutines.launch

class ValueActivity3 : BaseActivity() {

    private val valueAdapter = ValueAdapter3()
    private var index = 0

    private val viewModel by viewModels<CarValueVM>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityValue3Binding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.recyclerView.adapter = valueAdapter


        binding.apply {

            ivTitle.text = intent.getStringExtra("title")
            index = intent.getIntExtra("index",0)

            ivBack.setOnClickListener {
                finish()
            }
        }

        lifecycleScope.launch {
            viewModel.carValueListStateFlow.collect{
                Log.i("sss===", "onCreate: collect $it")
                if (it != null) {
                    valueAdapter.data.clear()
                    valueAdapter.data.addAll(it)
                }
                valueAdapter.notifyDataSetChanged()
            }
        }


        viewModel.getCarValueList(index)


    }






}